//
//  FoodDataBase.m
//  Happy Kitchen
//
//  Created by dlios on 15/3/12.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "FoodDataBase.h"

@implementation FoodDataBase

+(instancetype)shareMenuDataBaseHandle{
    static FoodDataBase *menuBase = nil;
    if (menuBase == nil) {
        menuBase = [[FoodDataBase alloc] init];
    }
    return menuBase;
}

#pragma mark --使用数据库之前,先要往工程里倒入库文件libsqlite3.0.dylib文件;然后引入sqlite.h文件   --  第二步,引入头文件

#pragma mark --打开数据库

//创建数据库对象
static sqlite3 *menuDataBase;

//自定义方法打开数据库
- (void)openMenuDB
{
    //获取沙盒路径
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //取出document路径
    NSString *filePath = [array lastObject];
    //拼接一个sqlite路径
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"menuDetail.sqlite"];
    NSLog(@"sqlitePath = %@", sqlitePath);
    
    if (menuDataBase != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    //打开数据库方法
    int result = sqlite3_open(sqlitePath.UTF8String, &menuDataBase);
    
    //根据返回值来判断数据库是否打开成功
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
    
}

#pragma mark -- 创建数据库表

- (void)createMenuTable
{
    //生成创建表的Sql语句
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS foodTable (number INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, vegetable_id TEXT)";
    
    //执行sql语句
    int result = sqlite3_exec(menuDataBase, createSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }
    else
    {
        NSLog(@"创建表失败");
    }
    
}


#pragma mark -- 插入美食信息
- (void)insertMenu:(MenuDetail *)menuDetail
{
    //创建插入学生的sql语句
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO foodTable(name, vegetable_id) VALUES('%@', '%@')", menuDetail.name, menuDetail.vegetable_id];
    //执行sql语句
    int result = sqlite3_exec(menuDataBase, insertSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }
    else
    {
        NSLog(@"插入失败");
    }
}


#pragma makr -- 查询数据库信息
-(NSMutableArray *)selectMenu
{
    NSString *selectSql = @"SELECT * FROM foodTable";
    //创建数组跟随指针,循环遍历每行数据
    sqlite3_stmt *stmt = nil;
    //创建存放电影信息的数组
    NSMutableArray *array = [NSMutableArray array];
    int result = sqlite3_prepare(menuDataBase, selectSql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        //开始遍历数组
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *vegetable_id = sqlite3_column_text(stmt, 2);
            
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            NSString *vegetable_idStr = [NSString stringWithUTF8String:(const char *)vegetable_id];
            MenuDetail *menuDetail = [[MenuDetail alloc] init];
            menuDetail.name = nameStr;
            menuDetail.vegetable_id = vegetable_idStr;
            //将美食放入数组中
            [array addObject:menuDetail];
        }
    }
    else
    {
        NSLog(@"查询失败");
    }
    return array;
}

#pragma mark -- 删除美食信息

- (void)deleteMenu:(NSString *)name
{
    NSString *deletaSql = [NSString stringWithFormat:@"DELETE FROM foodTable WHERE name = '%@'", name];
    
    int result = sqlite3_exec(menuDataBase, deletaSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
    else
    {
        NSLog(@"删除失败");
    }
    
}

#pragma mark -- 删除整个表
- (void)DropMenuTable
{
    NSString *dropSql =  @"DROP TABLE foodTable";
    int result = sqlite3_exec(menuDataBase, dropSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"清除表成功");
    }
    else
    {
        NSLog(@"清除表失败");
    }
    
}

#pragma mark -- 关闭数据库
- (void)closeMenuDB
{
    int result = sqlite3_close(menuDataBase);
    if (result == SQLITE_OK) {
        NSLog(@"关闭数据库成功");
        menuDataBase = nil;
    }
    else
    {
        NSLog(@"关闭数据库失败");
    }
    
}




















@end
